package cz.none.bezbarier.web.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SecurityUtils {

	private static final Logger LOG = LoggerFactory.getLogger(SecurityUtils.class);

	public static String passwordToMD5(String password) {
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			LOG.error("Realy bad error!! Couldn't hash user password.", e);
			throw new RuntimeException(e);
		}

		String finalPassword = password + Constants.Common.SALT;
		digest.update(finalPassword.getBytes(), 0, finalPassword.length());

		String hash = new BigInteger(1, digest.digest()).toString(16);
		if (hash.length() < 32) {
			hash = "0" + hash;
		}

		return hash;
	}
}
